package carassius.DAL;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;

/**
 *
 * @author siebz0r
 */
public abstract class ActionTable<T extends ActionRow>
		extends Table<ActionRow, Integer>
{
	private final String SQL_ADD = "INSERT INTO action VALUES ( NULL, ? , ? , ? )",
			SQL_DELETE = "DELETE FROM action WHERE id = ?";

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	//<editor-fold defaultstate="collapsed" desc="SQL add">
	public ActionRow add(Date date, String comment,
								   AquariumRow aquariumRow) throws SQLException
	{
		if (aquariumRow != null)
		{
			return add(date, comment, aquariumRow.getId());
		}
		return null;
	}

	public ActionRow add(Date date, String comment, Integer aquariumId) throws SQLException
	{
		Integer id = add(new Object[]
				{
					date, comment, aquariumId
				});
		if (id >= 0)
		{
			return getByPrimaryKey(id);
		}
		return null;
	}

	@Override
	public Integer add(Object[] args) throws SQLException
	{
		int out = -1;
		if (args != null
				&& args.length >=3
				&& args[0] instanceof Date
				&& ((args[2] instanceof AquariumRow && ((AquariumRow)args[2]).getId() >= 0)
					|| (args[2] instanceof Integer && ((Integer)args[2]) >= 0)))
		{
			PreparedStatement st = Database.prepareStatement(SQL_ADD);
			st.setDate(1, (Date) args[0]);
			st.setObject(2, (args[1] instanceof String) ? args[1] : null, Types.VARCHAR);
			st.setInt(3, (args[2] instanceof AquariumRow) ? ((AquariumRow)args[2]).getId() : (Integer)args[2]);

			synchronized (getRows())
			{
				try
				{
					if (st.executeUpdate() == 1)
					{
						ResultSet keys = st.getGeneratedKeys();

						if (keys.next())
						{
							int id = keys.getInt(1);
							if (!keys.wasNull())
							{
								out = id;
							}
						}
					}
				}
				catch (SQLException ex)
				{
					throw ex;
				}
				finally
				{
					st.close();
				}
			}
		}
		return out;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="SQL delete">
	@Override
	public boolean delete(Integer id) throws SQLException
	{
		boolean result = false;
		if (id != null && id >= 0)
		{
			PreparedStatement st = Database.prepareStatement(SQL_DELETE);
			st.setInt(1, id);

			synchronized (getRows())
			{
				try
				{
					if (st.executeUpdate() == 1)
					{
						result = true;
						getRows().remove(id);
					}
				}
				catch (SQLException ex)
				{
					throw ex;
				}
				finally
				{
					st.close();
				}
			}
		}
		return result;
	}

	public boolean deleteByAquariumId(int id) throws SQLException
	{
		boolean result = true;
		synchronized (getRows())
		{
			ArrayList<ActionRow> rQueue = getByAquariumId(id);
			for (ActionRow row : rQueue)
			{
				if (!delete(row.getId()))
				{
					result = false;
				}
			}
		}
		return result;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="SQL get">
	public abstract ArrayList<ActionRow> getByAquariumId(int ID) throws SQLException;
	//</editor-fold>
	//</editor-fold>
}
